package com.example.hcd.adapt;

import java.util.List;

import com.example.hcd.R;
import com.example.hcd.datamanage.ImageAndText;

import com.example.hcd.internet.FileUtils;
import com.example.hcd.tool.ViewCache;
import com.nostra13.universalimageloader.core.ImageLoader;


import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText>
{

	private static String TAG = "diaoliang";
	private static int URLCOUNT = 0;
	private GridView gridView;


	public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts, GridView gridView1) {
		super(activity, 0, imageAndTexts);
		this.gridView = gridView1;

		if (imageAndTexts.size() == 0)
		{
			gridView1.setVisibility(View.GONE);
		}

//		 asyncImageLoader = new AsyncImageLoader();
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		Activity activity = (Activity) getContext();

		// Inflate the views from XML
		View rowView = convertView;
		ViewCache viewCache;
		if (rowView == null)
		{
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.griditem, null);
			viewCache = new ViewCache(rowView);
			rowView.setTag(viewCache);
		} else
		{
			viewCache = (ViewCache) rowView.getTag();
		}
		ImageAndText imageAndText = getItem(position);

		// Load the image and set it on the ImageView
		String imageUrl = imageAndText.getImageUrl();
		final ImageView imageView = viewCache.getImageView();
		String textName = imageAndText.getText();

		// String Tag = imageUrl+ URLCOUNT;
		//
		// imageView.setTag(Tag);
//		URLCOUNT++;

		if (FileUtils.isPic(textName))
		{
		
			ImageLoader.getInstance().displayImage(imageUrl, imageView);
			// Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl,
			// Tag, new ImageCallback() {
			// public void imageLoaded(Drawable imageDrawable, String imageUrl,
			// String Tag)
			// {
			// ImageView imageViewByTag = (ImageView)
			// gridView.findViewWithTag(Tag);
			// if (imageViewByTag != null)
			// {
			// imageViewByTag.setImageDrawable(imageDrawable);
			// }
			// }
			// });

		} else
		{
			if (textName.endsWith(".pdf"))
			{
				imageView.setImageResource(R.drawable.pdf);
			} else if (textName.endsWith(".txt"))
			{
				imageView.setImageResource(R.drawable.text);
			} else if (textName.contains(".doc"))
			{
				imageView.setImageResource(R.drawable.word);
			} else if (textName.endsWith(".zip"))
			{
				imageView.setImageResource(R.drawable.zip);
			} else if (textName.contains(".xls"))
			{
				imageView.setImageResource(R.drawable.excel);
			} else
			{
				imageView.setImageResource(R.drawable.file);
			}
		}

		String text = "";
		if (FileUtils.isPic(textName))
		{
			text = "photo" + getnum(true, position);
		} else
		{
			text = "file" + getnum(false, position) + getLast1(textName);
		}

		// Set the text on the TextView
		TextView textView = viewCache.getTextView();
		textView.setText(text);
		return rowView;
	}

	private int getnum(boolean ispng, int postion)
	{
		int num = 1;

		for (int i = 0; i < postion; i++)
		{
			if (ispng)
			{
				ImageAndText name = getItem(i);
				String textName = name.getText();
				if (FileUtils.isPic(textName))
				{
					num++;
				}

			} else
			{
				ImageAndText name = getItem(i);
				String textName = name.getText();
				if (FileUtils.isPic(textName)) {

				} else
				{
					num++;
				}

			}

		}

		return num;
	}

	private String getLast(String path)
	{
		String a[] = path.split("/");
		return a[a.length - 1];
	}

	private String getLast1(String path)
	{
		String a[] = path.split("\\.");
		if (a.length > 1)
		{
			return "." + a[a.length - 1];
		} else
			return "";

	}

}